﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using Microsoft.IdentityModel.OAuth;

namespace AMT.IdentityModel.OAuth.Tests
{
	[TestClass]
	public class TestSWTUserSymmetricKeyClaimToken
	{
		private TestParameters m_params;
		private SimpleWebToken m_swt;

		[TestInitialize]
		public void Initialize()
		{
			m_params = TestParameters.LoadFromConfig();
		}

		#region Valid (positive) tests
		[TestMethod]
		public void ValidUsername_ValidSymmetricKey()
		{
			AMT.IdentityModel.OAuth.WRAPHandler<Microsoft.IdentityModel.OAuth.SimpleWebToken, AMT.IdentityModel.OAuth.SWTUserSymmetricKeyClaimToken> wrapHandler = GetDefaultWrapHandler();
			Assert.IsNotNull(wrapHandler);
			wrapHandler.ClaimToken = new SWTUserSymmetricKeyClaimToken(m_params.IdentityName, m_params.IdentitySymmetricKey);

			Console.WriteLine(TestParameters.GetParamInfoForOutput(wrapHandler));
			m_swt = wrapHandler.RetrieveTokenFromSTS();
			Assert.IsNotNull(m_swt);
            Assert.IsTrue(m_params.STSBaseUri.ToString().Contains(m_swt.Issuer));
			Assert.AreEqual(m_params.Scope, m_swt.Audience);
		}
		#endregion


		#region Invalid (negative) tests
		[TestMethod]
		public void ValidUsername_InvalidSymmetricKey()
		{
			AMT.IdentityModel.OAuth.WRAPHandler<Microsoft.IdentityModel.OAuth.SimpleWebToken, AMT.IdentityModel.OAuth.SWTUserSymmetricKeyClaimToken> wrapHandler = GetDefaultWrapHandler();
			Assert.IsNotNull(wrapHandler);

			wrapHandler.ClaimToken = new SWTUserSymmetricKeyClaimToken(m_params.IdentityName, TestValueMangler.MangleSymmetricKey(m_params.IdentitySymmetricKey));

			try
			{
				Console.WriteLine(GetTestParamDataForOutput(wrapHandler));
				m_swt = wrapHandler.RetrieveTokenFromSTS();
			}
			catch (System.Net.WebException ex)
			{
				System.Diagnostics.Debug.WriteLine(string.Format("Status: {0}", ex.Status.ToString()));
				System.Diagnostics.Debug.WriteLine(string.Format("Message: {0}", ex.Message));
				Assert.IsTrue(ex.Message.Contains("(401)"));
				Assert.IsTrue(ex.Message.Contains("Unauthorized"));
			}
		}


		[TestMethod]
		public void InvalidUsername_ValidSymmetricKey()
		{
			m_params.IdentityName += 'X';

			AMT.IdentityModel.OAuth.WRAPHandler<Microsoft.IdentityModel.OAuth.SimpleWebToken, AMT.IdentityModel.OAuth.SWTUserSymmetricKeyClaimToken> wrapHandler = GetDefaultWrapHandler();
			Assert.IsNotNull(wrapHandler);

			wrapHandler.ClaimToken = new SWTUserSymmetricKeyClaimToken(m_params.IdentityName, m_params.IdentitySymmetricKey);

			try
			{
				Console.WriteLine(GetTestParamDataForOutput(wrapHandler));
				m_swt = wrapHandler.RetrieveTokenFromSTS();
			}
			catch (System.Net.WebException ex)
			{
				System.Diagnostics.Debug.WriteLine(string.Format("Status: {0}", ex.Status.ToString()));
				System.Diagnostics.Debug.WriteLine(string.Format("Message: {0}", ex.Message));
                Assert.IsTrue(ex.Message.Contains("(401)"));
                Assert.IsTrue(ex.Message.Contains("Unauthorized"));
            }
		}
		#endregion

		#region Private helper methods
		private AMT.IdentityModel.OAuth.WRAPHandler<Microsoft.IdentityModel.OAuth.SimpleWebToken, AMT.IdentityModel.OAuth.SWTUserSymmetricKeyClaimToken> GetDefaultWrapHandler()
		{
			AMT.IdentityModel.OAuth.WRAPHandler<Microsoft.IdentityModel.OAuth.SimpleWebToken, AMT.IdentityModel.OAuth.SWTUserSymmetricKeyClaimToken> wrapHandler =
						 new AMT.IdentityModel.OAuth.WRAPHandler<SimpleWebToken, AMT.IdentityModel.OAuth.SWTUserSymmetricKeyClaimToken>(m_params.STSBaseUri, m_params.Scope);
			return wrapHandler;
		}


		private string GetTestParamDataForOutput(AMT.IdentityModel.OAuth.WRAPHandler<Microsoft.IdentityModel.OAuth.SimpleWebToken, AMT.IdentityModel.OAuth.SWTUserSymmetricKeyClaimToken> wh)
		{
//			return new System.Text.StringBuilder(1024).AppendFormat("STSBaseUri: {0}", wh.STSUri.AbsoluteUri).AppendFormat("Scope: {0}", wh.Scope).ToString();
			return string.Format("Scope: {0}; STSBaseUri: {1};", wh.Scope, wh.STSUri.AbsoluteUri);
		}
		#endregion
	}
}
